package cn.edu.scau.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.config.Audience;
import cn.edu.scau.entity.Care;
import cn.edu.scau.entity.Customer;
import cn.edu.scau.entity.Manager;
import cn.edu.scau.entity.Nurse;
import cn.edu.scau.service.CareService;
import cn.edu.scau.service.CustomerService;
import cn.edu.scau.service.ManagerService;
import cn.edu.scau.service.NurseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "照顾信息控制器")
@RestController
@Controller
@RequestMapping("care")
public class CareController {
	@Autowired
	CareService careService;
	@Autowired
	NurseService nurseService;
	@Autowired
	CustomerService customerService;
	@Autowired
	ManagerService managerService;
	@Autowired
	private Audience audience;//?
	
	/**
	 * 显示所有照顾信息
	 * @return
	 */
	@ApiOperation(value = "显示所有信息页面:数据存储在hospital中")
    @GetMapping(value = "/list")
    public ResultBean list(){
		List<Care> cares = careService.selectAll();
        System.out.println("请求所有照顾信息数据："+cares);
        ResultBean r = new ResultBean(200,true,"查询所有照顾信息成功",cares);
        return r;
	}
	
	@ApiOperation(value = "显示所有信息页面:数据存储在hospital中")
    @GetMapping(value = "/nurseList/{userAccount}")
    public ResultBean nurseList(@PathVariable("userAccount") String userAccount){
		Manager manager = managerService.selectByUserAccount(userAccount);
		Nurse nurse = nurseService.selectByUserId(manager.getUserId());
		List<Care> cares = careService.selectByNurseID(nurse.getNurseid());
        System.out.println("请求所有照顾信息数据："+cares);
        ResultBean r = new ResultBean(200,true,"查询所有照顾信息成功",cares);
        return r;
	}
	
	/**
	 * 根据这件查询
	 * @param customerid
	 * @return
	 */
	@GetMapping(value = "/{id}")
	public ResultBean findByID(@PathVariable("id") Integer customerid){
		ResultBean r = null;
		Care care = careService.selectByPrimaryKey(customerid);
		if(care==null) {
			r = new ResultBean(500,false,"查询照顾信息失败",null);
		}
		else {
			r = new ResultBean(200,true,"查询照顾信息成功",care);
		}
		return r;
	}
	
	/**
	 * 修改照顾信息
	 * @param customerid
	 * @param care
	 * @param bindingResult
	 * @return
	 */
	@PutMapping(value = "/update/{id}")
    public ResultBean update(@PathVariable("id") Integer customerid,@Validated Care care, BindingResult bindingResult) {
        ResultBean r = null;
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError f:bindingResult.getFieldErrors()
            ) {
                msg.append(f.getField()+":"+f.getDefaultMessage()+"\n");
            }
            r = new ResultBean(5006,false,msg.toString(),null);
            return r;
        }
        
        Customer customer = customerService.selectByPrimaryKey(care.getCustomerid());
        if(customer==null) {
        	r =new ResultBean(5005,false,"客户编号不存在或无效",null);
        	return r;
        }
        else {
        	if(customer.getCustomeruseflag()==false) {
        		r =new ResultBean(5005,false,"客户编号不存在或无效",null);
        		return r;
        	}
        }//外键约束
        Nurse nurse=nurseService.selectByPrimaryKey(care.getNurseid());
        if(nurse==null) {
        	r =new ResultBean(5005,false,"护士编号不存在或无效",null);
        	return r;
        }
        else {
        	if(nurse.getNurseuseflag()==false) {
        		r =new ResultBean(5005,false,"护士编号不存在或无效",null);
        		return r;
        	}
        }//外键约束
        
        if(careService.selectByPrimaryKey(care.getCustomerid())!=null) {
        	r =new ResultBean(5005,false,"已经有对应客户的照顾信息",null);
        	return r;
        }
        
        boolean flag = careService.update(care);
        if(flag){
            r =new ResultBean(200,true,"修改成功",null);
        }else
        {
            r =new ResultBean(5005,false,"修改失败",null);
        }
        return r;
    }
	
	
	/**
	 * 根据前端发来的护士编号检测该护士是否存在
	 * @param nurseid
	 * @return
	 */
	@GetMapping(value = "/checkNurseID/{nurseid}")
	public ResultBean checkNurseID(@PathVariable("nurseid") Integer nurseid){
		ResultBean r = null;
		Nurse nurse = nurseService.selectByPrimaryKey(nurseid);
		if(nurse==null) { 
			r = new ResultBean(500,false,"找不到编号为"+nurseid+"的护士，请确认后重新输入",null);
			return r;
		}
		else {
			if(nurse.getNurseuseflag()==false) {
				r = new ResultBean(500,false,"已经没有编号为"+nurseid+"的护士，请确认后重新输入",null);
				return r;
			}
		}
		r = new ResultBean(200,true,"护士编号检查成功",nurse.getNursename());
		return r;
	}
	
	/**
	 * 根据前端发来的客户编号检测该客户是否存在
	 * @param customerid
	 * @return
	 */
	@GetMapping(value = "/checkCustomerID/{customerid}")
	public ResultBean checkCustomerID(@PathVariable("customerid") Integer customerid){
		ResultBean r = null;
		Customer customer = customerService.selectByPrimaryKey(customerid);
		if(customer==null) { 
			r = new ResultBean(500,false,"找不到编号为"+customerid+"的客户，请确认后重新输入",null);
			return r;
		}
		else {
			if(customer.getCustomeruseflag()==false) {
				r = new ResultBean(500,false,"已经没有编号为"+customerid+"的客户，请确认后重新输入",null);
				return r;
			}
		}
		r = new ResultBean(200,true,"客户编号检查成功",customer.getCustomername());
		return r;
	}
	
	
	/**
	 * 新增照顾信息
	 * @param care
	 * @param bindingResult
	 * @return
	 */
	@PostMapping(value = "/insert")
    public ResultBean insert(@Validated Care care, BindingResult bindingResult) {
		ResultBean r = null;
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError f:bindingResult.getFieldErrors()
                 ) {
                msg.append(f.getField()+":"+f.getDefaultMessage()+"\n");
            }
            r = new ResultBean(5006,false,msg.toString(),null);
            return r;
        }
        
        Customer customer = customerService.selectByPrimaryKey(care.getCustomerid());
        if(customer==null) {
        	r =new ResultBean(5005,false,"客户编号不存在或无效",null);
        	return r;
        }
        else {
        	if(customer.getCustomeruseflag()==false) {
        		r =new ResultBean(5005,false,"客户编号不存在或无效",null);
        		return r;
        	}
        }//外键约束
        Nurse nurse=nurseService.selectByPrimaryKey(care.getNurseid());
        if(nurse==null) {
        	r =new ResultBean(5005,false,"护士编号不存在或无效",null);
        	return r;
        }
        else {
        	if(nurse.getNurseuseflag()==false) {
        		r =new ResultBean(5005,false,"护士编号不存在或无效",null);
        		return r;
        	}
        }//外键约束
        
        if(careService.selectByPrimaryKey(care.getCustomerid())!=null) {
        	r =new ResultBean(5005,false,"已经有对应客户的照顾信息",null);
        	return r;
        }
        
        boolean flag = careService.insert(care);
        if(flag){
            r =new ResultBean(200,true,"增加成功\n新增客户编号为"+care.getCustomerid()+"照顾信息",null);
        }else
        {
            r =new ResultBean(5005,false,"增加失败",null);
        }
        return r;
	}
	
	/**
	 * 根据主键删除照顾信息
	 * @param customerid
	 * @return
	 */
	@DeleteMapping(value = "/delete/{id}")
    public ResultBean delete(@PathVariable("id") Integer customerid){
		ResultBean r = null;
        boolean flag = careService.delete(customerid);
        if(flag){
            r =new ResultBean(200,true,"删除成功",null);
        }else
        {
            r =new ResultBean(5005,false,"删除失败",null);
        }
        return r;
	}
	
	/**
	 * 根据客户名、客户编号、护士名、护士编号组合查询
	 * @param customerid
	 * @param customername
	 * @param nurseid
	 * @param nursename
	 * @return
	 */
	@GetMapping(value = "/find")
    public ResultBean find(Integer customerid, String customername, Integer nurseid, String nursename){
        List<Care> cares = careService.find(customerid, customername, nurseid, nursename);
        ResultBean r = new ResultBean(200,true,"组合模糊查询照顾信息成功",cares);
        return r;
    }
	
	@GetMapping(value = "/nurseFind/{userAccount}")
    public ResultBean find(@PathVariable("userAccount") String userAccount,Integer customerid, String customername){
		Manager manager = managerService.selectByUserAccount(userAccount);
		Nurse nurse = nurseService.selectByUserId(manager.getUserId());
        List<Care> cares = careService.findWithNurseID(customerid, customername, nurse.getNurseid(), null);
        ResultBean r = new ResultBean(200,true,"组合模糊查询照顾信息成功",cares);
        return r;
    }
}
